<?php
/**
 * Zend Image Manipulation Module
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.

 * @category   Zimm
 * @package    Zimm
 * @copyright  Copyright (c) 2012 Kristoffer Ingemansson
 * @license    New BSD License
 * @version    $Id: Overlay.php 4 2012-04-14 12:59:16Z kristoffer.ingemansson@gmail.com $
 */

class Zimm_Model_DbTable_Instruction_Overlay extends Zimm_Model_DbTable_Instruction_Abstract
{
    /**
     * Load new image and overlay it on top of image.
     *
     * @param resource $image GD resource
     * @param string $savePath Target save path
     * @param array $params Instruction arguments
     * @return boolean Indicates if image was altered
     */
    protected function _process(&$image, $savePath, $params)
    {
        if (isset($params['image'])) {
            if ($data = $this->_loadFile($params['image'])) {
                if ($overlayImage = imagecreatefromstring($data)) {

                    $left = (isset($params['left']) ? $params['left'] : NULL);
                    $right = (isset($params['right']) ? $params['right'] : NULL);
                    $top = (isset($params['top']) ? $params['top'] : NULL);
                    $bottom = (isset($params['bottom']) ? $params['bottom'] : NULL);

                    $imageWidth = imagesx($image);
                    $imageHeight = imagesy($image);

                    $overlayWidth = imagesx($overlayImage);
                    $overlayHeight = imagesy($overlayImage);

                    $x = $imageWidth / 2 - $overlayWidth / 2;
                    $y = $imageHeight / 2 - $overlayHeight / 2;

                    if ($left !== NULL) {
                        if (strpos($left, '%')) {
                            $left = (float)$left / 100;
                            $x = (int)($imageWidth * $left - $overlayWidth * $left);
                        } else {
                            $x = (int)$left;
                        }
                    } else if ($right !== NULL) {
                        if (strpos($right, '%')) {
                            $right = (float)$right / 100;
                            $x = ($imageWidth - $imageWidth * $right) + $overlayWidth * $right - $overlayWidth;
                        } else {
                            $x = $imageWidth - $overlayWidth - (int)$right;
                        }
                    }

                    if ($top !== NULL) {
                        if (strpos($top, '%')) {
                            $top = (float)$top / 100;
                            $y = (int)($imageHeight * $top - $overlayHeight * $top);
                        } else {
                            $y = (int)$top;
                        }
                    } else if ($bottom !== NULL) {
                        if (strpos($bottom, '%')) {
                            $bottom = (float)$bottom / 100;
                            $y = ($imageHeight - $imageHeight * $bottom) + $overlayHeight * $bottom - $overlayHeight;
                        } else {
                            $y = $imageHeight - $overlayHeight - (int)$bottom;
                        }
                    }

                    imagealphablending($image, true);
                    imagecopyresampled($image, $overlayImage, $x, $y, 0, 0, $overlayWidth, $overlayHeight, $overlayWidth, $overlayHeight);
                    imagedestroy($overlayImage);

                    return true;
                }
            }
        }

        return false;
    }
}